const TreeNode = require('./treeNode.js').TreeNode;

/**
 * 树构造器
 * 
 * @author shuyuan.dai 2021-03-25
 */
class TreeBuilder {
    static build(array) {
        let treeNodes = [];
        for(let i=0; i < array.length; ++i) {
            let node = new TreeNode(array[i]);
            treeNodes[i] = node;
            if(i <= 0) {
                continue;
            }
            let parentIndex = (i - 1) >>> 1, 
                parent = treeNodes[parentIndex];
            node.parent = parent;
            if((i & 1) == 0) {
                parent.lchild = node;
            } else {
                parent.rchild = node;
            }
        }
        return treeNodes[0];
    }
}

exports.TreeBuilder = TreeBuilder;